package com.siqi.hr.system.common.dao.mysql;

import com.siqi.hr.common.utils.StringUtil;
import com.siqi.hr.system.common.dao.intf.RecyclebinDao;
import com.siqi.hr.system.common.sql.STable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * 处理回收站通用dao层实现类
 * @version 2016-08-18
 * @author xiaogq
 * @since JDK1.8.0_12, Spring3.0.2
 */
@Repository("recyclebinDao")
public class RecyclebinDaoImpl implements RecyclebinDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int recyclebinHandel(Integer entity_id, String records, Integer code) {
        Assert.notNull(entity_id);
        Assert.hasText(records);
        int counts = 0;
        StringBuilder sql = new StringBuilder();
        if (code == 1) {//如果是恢复选中记录
            sql.append(" update " + STable.TABLE_HEAD + entity_id + " set valid_data_bit = true where id in (" + StringUtil.getString(records) + ")");
        }else if (code == 2) {//如果是恢复所有记录
            sql.append(" update " + STable.TABLE_HEAD + entity_id + " set valid_data_bit = true where valid_data_bit = false ");
        }else if (code == 3) {//如果是删除选中记录
            sql.append(" delete from " + STable.TABLE_HEAD + entity_id + " where valid_data_bit = false and id in (" + StringUtil.getString(records) + ")");
        }else {//如果是删除所有记录
            sql.append(" delete from " + STable.TABLE_HEAD+entity_id+ " where valid_data_bit = false ");
        }
        try {
            counts = jdbcTemplate.update(sql.toString());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return counts;
    }
}
